When (and why) is {} undefined in a JavaScript console?

Posted by JS_Riddler on Stack Overflow See other posts from Stack Overflow or by JS_Riddler
Published on 2012-03-29T22:21:46Z Indexed on 2012/03/29 23:29 UTC
Read the original article Hit count: 169

Filed under:
|

In the console of both FF and Chrome, {} is considered undefined until explicitly evaluated:

{};     // undefined
({});   // ? Object

Actually, it's a bit less defined than undefined -- it's apparently bad syntax:

{} === undefined;  // SyntaxError: Unexpected token ===
{}.constructor;    // SyntaxError: Unexpected token .

But not if it's on the other side, in which case it's fine:

"[object Object]" == {}.toString(); // true

Or if it's not the first expression:

undefined + undefined; // NaN
{} + undefined;        // NaN
undefined + {};        // "undefined[object Object]"

What gives?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about console